// ------------------------------------------------------------------------------
// name: 报警情况统计
// author: cookie( h5devs.com/h5devs.net )
// created: 2018/12/1 22:30
// ------------------------------------------------------------------------------
// state
const State = {
  commData: [],
  data: {
    case: [],
    type: []
  }
};

// getters
const Getters = {
  commData: state => state.commData,
  getCase: state => state.data['case'],
};

// mutations
const Mutations = {
  setSource(state, commData) {
    state.commData = commData;
  },
  pushItem(state, item, type = 'case') {
    if (!state.data[type]) {
      state.data[type] = [];
    }
    let _data = state.data[type];
    // 判断是否有重复字段key,如果有，更新数据，没有就添加
    let bool = false;
    let index = 0;
    for (let i = 0; i < _data.length; i++) {
      let _item = _data[i];
      if (_item.key === item.key) {
        bool = true;
        index = i;
        break;
      }
    }
    if (bool) {
      _data.splice(index, 1, item);
      return;
    }
    _data.push(item);
    console.warn(state.data[type]);
  },
  pushItem2(state, item) {
    // 判断是否有重复字段key,如果有，更新数据，没有就添加
    let bool = false;
    let index = 0;
    for (let i = 0; i < state.commData.length; i++) {
      let _item = state.commData[i];
      if (_item.key === item.key) {
        bool = true;
        index = i;
        break;
      }
    }
    if (bool) {
      state.commData.splice(index, 1, item);
      return;
    }
    state.commData.push(item);
    console.warn(state.commData);
  },
  deleteItem(state, index) {
    state.commData.splice(index, 1);
  }
};

// actions
const Actions = {
  setCommData({
    commit
  }, commData) {
    commit('setSource', commData);
  },
  setItem({
    commit
  }, itemData, type) {
    commit('pushItem', itemData, type);
  },
  removeItem({
    commit
  }, index) {
    commit('deleteItem', index);
  }
};

export default {
  namespaced: true,
  strict: true,
  state: State,
  getters: Getters,
  actions: Actions,
  mutations: Mutations
};
